import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/Login.vue'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
    },
    {
      path: '/allTotal',
      name: 'allTotal',
      component: () => import('../views/view.vue'),
      redirect: '/allTotal/HelloWorld',
      children: [
        {
          path: 'HelloWorld',
          component: () => import('@/components/HelloWorld.vue'),
        },
        {
          path: 'Class',
          component: () => import('@/components/Total/Class.vue'),
        },
        {
          path: 'Business',
          component: () => import('@/components/Total/Business.vue'),
        },
        {
          path: 'Leave',
          component: () => import('@/components/Total/Leave.vue'),
        },
        {
          path: 'Work',
          component: () => import('@/components/Total/Work.vue'),
        },
        {
          path: 'Log',
          component: () => import('@/components/Total/Log.vue'),
        },
        {
          path: 'Examine',
          component: () => import('@/components/Total/Examine.vue'),
        },
        {
          path: 'Daily',
          component: () => import('@/components/Total/Daily.vue'),
        },
        {
          path: 'User',
          component: () => import('@/components/Total/User.vue'),
        },
        {
          path: 'Project',
          component: () => import('@/components/Total/Project.vue'),
        },
        {
          path: 'StudentFiles',
          component: () => import('@/components/Total/StudentFiles.vue'),
        },
        {
          path: 'Department',
          component: () => import('@/components/Total/Department.vue'),
        },
        {
          path: 'Subject',
          component: () => import('@/components/Total/Subject.vue'),
        },
        {
          path: 'ClassDetails',
          component: () => import('@/components/Total/ClassDetails.vue'),
        },
        {
          path: 'PreEnrollment',
          component: () => import('@/components/Total/PreEnrollment.vue'),
        },
        {
          path: 'Graduate',
          component: () => import('@/components/Total/Graduate.vue'),
        },
        {
          path: 'ProjectDetails',
          component: () => import('@/components/Total/ProjectDetails.vue'),
        },
        {
          path: 'SonProjectDetails',
          component: () => import('@/components/Total/SonProjectDetails.vue'),
        },
        {
          path: 'System',
          component: () => import('@/components/Total/System.vue'),
        },
        {
          path: 'SalesStatistics',
          component: () => import('@/components/Total/SalesStatistics.vue'),
        },
        {
          path: 'StudentData',
          component: () => import('@/components/Total/StudentData.vue'),
        },
        {
          path: 'ExcelData',
          component: () => import('@/components/Total/ExcelData.vue'),
        },
        {
          path: 'StudentLeave',
          component: () => import('@/components/Total/StudentLeave.vue'),
        },
        {
          path: 'StudentLog',
          component: () => import('@/components/Total/StudentLog.vue'),
        },
        {
          path: 'SInformation',
          component: () => import('@/components/Total/SInformation.vue'),
        }
      ]
    },
  ],
})
// 添加全局路由守卫
router.beforeEach((to, from, next) => {
  // 白名单页面允许直接访问
  const whiteList = ['/']
  const token = !!sessionStorage.getItem('token')
  // 获取登录状态（示例使用localStorage）
  const isLoggedIn = !!localStorage.getItem('user')
  
  if (whiteList.includes(to.path)) {
    next()
  } else if (!token){
    next('/')
  } else {
    next()
  }
})
export default router
